package com.sinogrand.elevator.utils

import android.content.Context
import android.content.SharedPreferences
import android.util.Log

/**
 * 环境管理器
 * 管理正式环境和测试环境的切换
 */
class EnvironmentManager(private val context: Context) {
    
    companion object {
        private const val PREFS_NAME = "environment_prefs"
        private const val KEY_ENVIRONMENT = "current_environment"
        private const val KEY_BASE_URL = "base_url"
        
        // 环境定义
        const val ENVIRONMENT_PRODUCTION = "production"
        const val ENVIRONMENT_TEST = "test"
        
        // 环境配置
        private val ENVIRONMENTS = mapOf(
            ENVIRONMENT_PRODUCTION to EnvironmentConfig(
                name = "正式环境",
                baseUrl = "https://api.sino-grand.com",
                approvalFlowUrl = "https://flow.sino-grand.com",
                testPhone = "",
                testPassword = ""
            ),
            ENVIRONMENT_TEST to EnvironmentConfig(
                name = "测试环境", 
                baseUrl = "https://api-test.sino-grand.com",
                approvalFlowUrl = "https://flow-test.sino-grand.com",
//                approvalFlowUrl = "http://192.168.5.65:8080",
                testPhone = "18895318888",
                testPassword = "12345678a"
            )
        )
    }
    
    private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
    
    /**
     * 获取当前环境
     */
    fun getCurrentEnvironment(): String {
        return prefs.getString(KEY_ENVIRONMENT, ENVIRONMENT_PRODUCTION) ?: ENVIRONMENT_PRODUCTION
    }
    
    /**
     * 设置环境
     */
    fun setEnvironment(environment: String) {
        if (environment in ENVIRONMENTS.keys) {
            prefs.edit().putString(KEY_ENVIRONMENT, environment).apply()
            Log.i(LogTags.ENVIRONMENT_MANAGER, "✅ 环境已切换到: ${ENVIRONMENTS[environment]?.name}")
        } else {
            Log.e(LogTags.ENVIRONMENT_MANAGER, "❌ 无效的环境: $environment")
        }
    }
    
    /**
     * 获取当前环境的配置
     */
    fun getCurrentEnvironmentConfig(): EnvironmentConfig {
        val currentEnv = getCurrentEnvironment()
        return ENVIRONMENTS[currentEnv] ?: ENVIRONMENTS[ENVIRONMENT_TEST]!!
    }
    
    /**
     * 获取所有可用环境
     */
    fun getAllEnvironments(): Map<String, EnvironmentConfig> {
        return ENVIRONMENTS
    }
    
    /**
     * 获取当前环境的API基础URL
     */
    fun getCurrentBaseUrl(): String {
        return getCurrentEnvironmentConfig().baseUrl
    }
    
    /**
     * 获取当前环境的审批流程URL
     */
    fun getCurrentApprovalFlowUrl(): String {
        return getCurrentEnvironmentConfig().approvalFlowUrl
    }
    
    /**
     * 检查是否为正式环境
     */
    fun isProduction(): Boolean {
        return getCurrentEnvironment() == ENVIRONMENT_PRODUCTION
    }
    
    /**
     * 检查是否为测试环境
     */
    fun isTest(): Boolean {
        return getCurrentEnvironment() == ENVIRONMENT_TEST
    }
    
    /**
     * 获取当前环境的测试账号手机号
     */
    fun getCurrentTestPhone(): String {
        return getCurrentEnvironmentConfig().testPhone
    }
    
    /**
     * 获取当前环境的测试账号密码
     */
    fun getCurrentTestPassword(): String {
        return getCurrentEnvironmentConfig().testPassword
    }
    
    /**
     * 清空环境相关数据（切换环境时调用）
     */
    fun clearEnvironmentData() {
        // 这里可以添加清空其他相关数据的逻辑
        Log.i(LogTags.ENVIRONMENT_MANAGER, "🧹 环境数据已清空")
    }
}

/**
 * 环境配置数据类
 */
data class EnvironmentConfig(
    val name: String,
    val baseUrl: String,
    val approvalFlowUrl: String,
    val testPhone: String = "",
    val testPassword: String = ""
)
